package cc.rengu.oltp.service.realize.impl;

import cc.rengu.oltp.service.common.dao.TxnSafMsgMapper;
import cc.rengu.oltp.service.common.dao.impl.TxnSafMsgMapperImpl;
import cc.rengu.oltp.service.common.entity.TxnSafMsg;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.TransSafMsgService;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by 王成 on 2020/2/19
 */
public class TransSafMsgServiceImpl implements TransSafMsgService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public void registerTransSafMsgInfo(String safGapType, int repeatTime, int repeatNum) {
        try {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String xmlString = xmlTreeUtil.toString();
            if (xmlString.length() > 8000) {
                rglog.error("存储转发报文超过8K，暂不登记到存储转发表:<{}>", xmlString);
                return;
            }
            TxnSafMsg txnSafMsg = new TxnSafMsg();
            ConvertUtil.convertOutput(txnSafMsg);
            txnSafMsg.setSafGapType(safGapType);
            txnSafMsg.setSafSendTime(repeatTime);
            txnSafMsg.setTotalCount(repeatNum);
            txnSafMsg.setSendCount(0);
            txnSafMsg.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
            if (xmlString.length() < 4000) {
                txnSafMsg.setSafMsg(xmlString);
            } else {
                txnSafMsg.setSafMsg(xmlString.substring(0, 4000));
                txnSafMsg.setSafMsgAdd(xmlString.substring(4000));
            }
            TxnSafMsgMapper txnSafMsgMapper = new TxnSafMsgMapperImpl();
            int iReturnCode = txnSafMsgMapper.insertTxnSafMsg(txnSafMsg);
            if (0 != iReturnCode) {
                rglog.error("登记存储转发表失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>,存储内容:<{}>",
                        txnSafMsg.getInstId(), txnSafMsg.getMsgSrcId(), txnSafMsg.getTxnNum(), txnSafMsg.getSysSeqNum(), iReturnCode, xmlString);
            }
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("登记存储转发表异常:<{}>", byteArrayOutputStream.toString());
        }
    }

    @Override
    public void loadTransSafMsgToXmlTree(String instId, String sysSeqNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        /* Step1：获取存储转发交易信息 */
        TxnSafMsgMapper txnSafMsgMapper = new TxnSafMsgMapperImpl();
        TxnSafMsg txnSafMsg = txnSafMsgMapper.selectTxnSafMsgByPrimaryKey(instId, sysSeqNum);
        if (null == txnSafMsg) {
            rglog.error("法人机构<{}>交易流水<{}>未登记存储转发表.", instId, sysSeqNum);
            throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND);
        }
        /* Step2：将存储转发信息转换到内部XML树 */
        String xmlString = txnSafMsg.getSafMsg();
        if (null != txnSafMsg.getSafMsgAdd() && !txnSafMsg.getSafMsgAdd().isEmpty()) {
            xmlString += txnSafMsg.getSafMsgAdd();
        }
        xmlTreeUtil.loadXmlString(xmlString);
    }
}
